<template>
  <!-- 商品素材 -->
  <div id="detailMaterial" class="goods-material-container">
    <div class="material-item" v-for="(item,index) in detailMaterial" :key="index">
      <div class="info">
        <div class="info-left">
          <img :src="item.avatar" alt mode="scaleToFill" />
        </div>
        <div class="info-right">
          <div class="name">{{item.title}}</div>
          <div class="time">{{item.update_time}}</div>
        </div>
      </div>
      <div class="content" v-if="item.content">{{item.content}}</div>
      <div class="img-box" v-if="item.imgs && item.imgs.length>0">
        <div class="has-img" v-for="(img,i) in item.imgs" :key="i">
          <img :src="img" alt="" @click="handlePreview(item.imgs,img)">
        </div>
        <div class="empty-img"></div>
        <div class="empty-img"></div>
        <div class="empty-img"></div>
      </div>
      <div class="btn-box">
        <!-- 有文字没有图片 -->
        <van-button
          class="btn share-btn"
          round
          @click="clickCopy(item.content,$event)"
          v-if="item.content.length>0 && (!item.imgs || item.imgs.length<=0)"
        >复制文字</van-button>
        <!-- 有图片没有文字 -->
        <van-button
          class="btn share-btn"
          round
          @click="saveImg(item.imgs)"
          v-if="item.content.length<=0 && item.imgs && item.imgs.length>0"
        >一键保存图片</van-button>
        <!-- 有文字也有图片 -->
        <van-button
          class="btn share-btn"
          round
          @click="copySaveImg(item.content,$event,item.imgs)"
          v-if="item.content.length>0 && item.imgs && item.imgs.length>0"
        >保存并复制</van-button>
      </div>
      
    </div>
  </div>
</template>

<script>
import { itemMaterial } from '@/api/item/item'
import small from '@/smallapp/small'
import Vue from 'vue'

export default Vue.extend({
  props: {
    itemId: {
      type: [Number, String]
    }
  },
  data() {
    return {
      isWx: true,
      p: 1,
      detailMaterial: []
    }
  },
  methods: {
    onLoad() {
      itemMaterial({ item_id: this.itemId, p: this.p }).then((res) => {
        if (res.status == 1) {
          this.detailMaterial = this.detailMaterial.concat(res.data)
          this.p++
          if (res.data.length > 0) {
            this.onLoad()
          }
        } else {
          this.$Error(res.msg)
        }
      })
    },
    // 复制文案
    clickCopy(text, event,notoast) {
      // TODO 微分销复制与小程序差别
      wx.setClipboardData({
        data: text,
        success(res) {
          if(notoast){
            wx.hideToast()
          }else{
            wx.showToast({
              title: '复制成功',
              icon: 'none',
            });
          }
        }
      })
    },
    // 点击图片放大
    handlePreview(imgsArr, curImg) {
      // TODO 小程序图片预览
      wx.previewImage({
        current: curImg, //当前图片地址
        urls: imgsArr,  //所有要预览的图片的地址集合数组形式
      })
    },
    //小程序文案和图片一起保存
    copySaveImg(text, event,imgs){
      let notoast=true
      this.clickCopy(text,event,notoast)
      this.saveImg(imgs,'图片与文字保存成功')
    },
    //保存图片
    saveImg(imgs,tip) {
      const txt=tip?tip:'图片保存成功'
      small.saveImageQueue(imgs).then((res) => {
        //全部下载完成
        wx.showToast({
          title: txt,
          icon: 'none',
        });
      })
    }
  },
  created() {
    this.onLoad()
    
  }
})
</script>

<style lang="scss">
@import "src/styles/mixin";
.goods-material-container {
  .material-item {
    margin:20px 0;
    padding: 24px;
    border-radius: 20px;
    background: #fff;
    &:first-child {
      margin-top: 0;
    }
    .info {
      display: flex;
      .info-left {
        overflow: hidden;
        width: 68px;
        height: 68px;
        margin-right: 12px;
        border-radius: 50%;
        img {
          display: block;
          width: 100%;
          height: 100%;
        }
      }
      .info-right {
        height: 68px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        .name {
          font-size: 28px;
        }
        .time {
          font-size: 22px;
          color: #999;
        }
      }
    }
    .content {
      padding: 20px 0 0;
      font-size: 26px;
      line-height: 36px;
    }
    .img-box {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;
      padding-top: 20px;
      .has-img {
        overflow: hidden;
        width: 208px;
        height: 208px;
        margin-bottom: 20px;
        border-radius: 6px;
      }
      img {
        display: block;
        width: 100%;
      }
      .empty-img {
        width: 208px;
        height: 0;
      }
    }
    .btn-box {
      margin-top: 12px;
      text-align: right;
      font-size: 0;
      .btn {
        height: 48px;
        padding: 0 18px;
        font-size: 22px;
      }
      .copy-btn {
        color: #f40c24;
        border-color: #f40c24;
      }
      .share-btn {
        margin-left: 20px;
        color: #fff;
        border: 0;
        background: #f40c24;
      }
    }
  }
}
</style>
